package com.example.droideep.fragment.animstudy;

import android.animation.LayoutTransition;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.GridLayout;

import com.example.droideep.R;
import com.example.droideep.common.activities.SimpleFragmentBase;

import butterknife.InjectView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

public class LayoutAnimationsFragment extends SimpleFragmentBase {

    @InjectView(R.id.id_container)
    ViewGroup mContainer;
//    @InjectView(R.id.id_appearing)
//    AppCompatCheckBox mAppearing;
//    @InjectView(R.id.id_change_appearing)
//    AppCompatCheckBox mChangeAppearing;
//    @InjectView(R.id.id_disappearing)
//    AppCompatCheckBox mDisappearing;
//    @InjectView(R.id.id_change_disappearing)
//    AppCompatCheckBox mChangeDisappearing;

    GridLayout mGridLayout;

    LayoutTransition mTransition;

    private int mVal;

    public static final String TAG = LayoutAnimationsFragment.class.getSimpleName();

    public static LayoutAnimationsFragment newInstance() {
        LayoutAnimationsFragment fragment = new LayoutAnimationsFragment();
        return fragment;
    }

    @Override
    protected int getContentResId() {
        return R.layout.fragment_layout_animations;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mGridLayout = new GridLayout(getActivity());
        mGridLayout.setColumnCount(5);
        mContainer.addView(mGridLayout);
        mGridLayout.setLayoutTransition(mTransition = new LayoutTransition());
    }

    @OnClick(R.id.add_btn)
    public void add() {
        final AppCompatButton button = new AppCompatButton(getActivity());
        button.setText((++mVal) + "");
        mGridLayout.addView(button, Math.min(1, mGridLayout.getChildCount()));

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mGridLayout.removeView(button);
            }
        });
    }

    @OnCheckedChanged({R.id.id_appearing, R.id.id_change_appearing, R.id.id_disappearing, R.id.id_change_disappearing})
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        mTransition = new LayoutTransition();
        switch (buttonView.getId()) {
            case R.id.id_appearing:
                mTransition.setAnimator(LayoutTransition.APPEARING,
                        (isChecked ? mTransition.getAnimator(LayoutTransition.APPEARING) : null));
                break;
            case R.id.id_change_appearing:
                mTransition.setAnimator(LayoutTransition.CHANGE_APPEARING,
                        (isChecked ? mTransition.getAnimator(LayoutTransition.CHANGE_APPEARING) : null));
                break;
            case R.id.id_disappearing:
                mTransition.setAnimator(LayoutTransition.DISAPPEARING,
                        (isChecked ? mTransition.getAnimator(LayoutTransition.DISAPPEARING) : null));
                break;
            case R.id.id_change_disappearing:
                mTransition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,
                        (isChecked ? mTransition.getAnimator(LayoutTransition.CHANGE_DISAPPEARING) : null));
                break;
        }
        mGridLayout.setLayoutTransition(mTransition);
    }
}
